package com.trans.auxiliary;


import com.trans.anno.TransTemplate;
import com.trans.handler.TranslateHandle;
import com.trans.utils.ModelUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author xwduo
 * @Description //TODO
 * @Date 2023-05-30 16:16
 */
public class TranslatorRecord {

    /**
     * 源生对象（源数据）
     */
    public final Object fullObject;

    /**
     * 当前层级处理的对象（子对象）
     */
    public final Object curObj;

    /**
     * 处理数据记录列表
     */
    public final Map<Class<? extends TranslateHandle>, List<HandleFullRecord>> handleRecordMap;

    /**
     * 其他参数
     */
    public final  Map<String, Object> otherMap;

    /**
     *  映射对象
     */
    public final  Map<String,Object> subEntry;

    /**
     * 已处理对象的hashCode => 防止循环遍历
     */
    private List<Integer> hashCodeList;

    /**
     * 模板Json
     */
    public final TransTemplate modelJson;

    private final Map<String, HandleFullRecord> modelMap;

    public TranslatorRecord(Object fullObject, Object curObj, Map<Class<? extends TranslateHandle>, List<HandleFullRecord>> handleRecordMap, Map<String, Object> otherMap, Map<String, Object> subEntry, TransTemplate modelJson) {
        this.fullObject = fullObject;
        this.curObj = curObj;
        this.handleRecordMap = handleRecordMap;
        this.otherMap = otherMap;
        this.subEntry = subEntry;
        this.modelJson = modelJson;
        this.modelMap = null != modelJson? ModelUtils.extractModelJson(modelJson.model()) : null;
        this.hashCodeList = new ArrayList<>();
    }

    public TranslatorRecord(Object fullObject, Object curObj, Map<Class<? extends TranslateHandle>, List<HandleFullRecord>> handleRecordMap, Map<String, Object> otherMap, Map<String, Object> subEntry, Class modelClass) {
        this.fullObject = fullObject;
        this.curObj = curObj;
        this.handleRecordMap = handleRecordMap;
        this.otherMap = otherMap;
        this.subEntry = subEntry;
        this.modelJson = null;
        this.modelMap = null != modelClass? ModelUtils.extractModelJson(modelClass) : null;
        this.hashCodeList = new ArrayList<>();
    }

    public TranslatorRecord(Object fullObject, Object curObj, Map<Class<? extends TranslateHandle>, List<HandleFullRecord>> handleRecordMap, Map<String, Object> otherMap, Map<String, Object> subEntry, TransTemplate modelJson , Class modelClass) {
        this.fullObject = fullObject;
        this.curObj = curObj;
        this.handleRecordMap = handleRecordMap;
        this.otherMap = otherMap;
        this.subEntry = subEntry;
        this.modelJson = modelJson;
        this.hashCodeList = new ArrayList<>();
        Map<String, HandleFullRecord> classMap = null != modelClass? ModelUtils.extractModelJson(modelClass) : null;
        if( null != modelJson){
            Map<String, HandleFullRecord> annoMap = ModelUtils.extractModelJson(modelJson.model());
            if(null != classMap){
                classMap.putAll(annoMap);
            }else {
                classMap = annoMap;
            }
        }
        this.modelMap = classMap;
    }

    public TranslatorRecord(Object fullObject, Object curObj, Map<Class<? extends TranslateHandle>, List<HandleFullRecord>> handleRecordMap, Map<String, Object> otherMap, Map<String, Object> subEntry, TransTemplate modelJson, Map<String, HandleFullRecord> modelMap) {
        this.fullObject = fullObject;
        this.curObj = curObj;
        this.handleRecordMap = handleRecordMap;
        this.otherMap = otherMap;
        this.subEntry = subEntry;
        this.modelJson = modelJson;
        this.modelMap = modelMap;
        this.hashCodeList = new ArrayList<>();
    }

    public TranslatorRecord copy(Object curObj){
        return new TranslatorRecord(this.fullObject,curObj,this.handleRecordMap,this.otherMap,null,this.modelJson,this.modelMap);
    }

    public TranslatorRecord copyNewMap(Object curObj){
        TranslatorRecord translatorRecord = new TranslatorRecord(this.fullObject, curObj, this.handleRecordMap, this.otherMap, new HashMap<>(), this.modelJson, this.modelMap);
        return translatorRecord.initHashCodeList(this.hashCodeList);
    }


    public TranslatorRecord initHashCodeList(List<Integer> hashCodeList){
        this.hashCodeList = hashCodeList;
        return this;
    }

    public boolean checkCodeExistAndRecord(Object o){
        int i = o.hashCode();
        if(hashCodeList.contains(i)){
            //该对象已经遍历过了 , 防止循环遍历
            return true;
        }else {
            hashCodeList.add(i);
            return false;
        }
    }

    public Map<String, HandleFullRecord> getModelMap() {
        return modelMap;
    }


}
